using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Box2D.XNA;
using Microsoft.Xna.Framework;
using Spinning.Spinning.Unitity;

namespace Spinning.Spinning.Entity
{
    class Bound:BaseEntity
    {
        Body body;

        public Vector2 Size { get; set; }

        public Bound() {
            Size = Vector2.Zero;
        }

        public void OnCreatBound(World world)
        {
            body = creatWall(world,Size , Vector2.Zero);
        }

        protected override void UpdateSelf(GameTime gameTime)
        {
            base.UpdateSelf(gameTime);
            setPosition(this.Position);
        }

        private void setPosition(Vector2 pos)
        {
            body.SetTransform(Functional.Vector2InWorld(pos), 0);
        }

        private Body creatWall(World world, Vector2 size, Vector2 poistion)
        {
            BodyDef bd = new BodyDef();
            bd.type = BodyType.Static;
            Body body = world.CreateBody(bd);
            FixtureDef fd = new FixtureDef();
            PolygonShape ps = new PolygonShape();
            ps.SetAsBox(size.X / Constants.MeterToPixal, size.Y / Constants.MeterToPixal);
            fd.shape = ps;
            Fixture f = body.CreateFixture(fd);
            Vector2 pos = Functional.Vector2InWorld(poistion);
            body.SetTransform(pos, 0);
            Filter filter = new Filter();
            filter.categoryBits = (ushort)Constants.CategoryBitsWall;
            filter.maskBits = (ushort)Constants.MaskBitsWall;
            f.SetFilterData(ref filter);
            return body;
        }
    }
}
